//
//  XMSexSelectView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/15.
//

#import "XMSexSelectView.h"

@interface XMSexSelectView ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation XMSexSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xf0f0f0);
    UITableView *tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(150.0)) style:UITableViewStylePlain];
    tbl.delegate = self;
    tbl.dataSource = self;
    tbl.separatorColor = kColorHEX(0xF1F1F1);
    tbl.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tbl.backgroundColor = kColorHEX(0xf0f0f0);
    if (@available(iOS 15.0, *)) {
        tbl.sectionHeaderTopPadding = 0;
    } else {
        tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        // Fallback on earlier versions
    }
    [self addSubview:tbl];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(50.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, KScaleWidth(50.0))];
    headeView.backgroundColor = kColorHEX(0xf0f0f0);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, headeView.width - KScaleWidth(16.0)*2, KScaleWidth(50.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    titleLabel.text = @"选择你的性别";
    [headeView addSubview:titleLabel];
    
    return headeView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(50.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMSexCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = KFontSize(17.0);
        cell.textLabel.textColor = kColorHEX(0x333333);
    }
    cell.textLabel.text = @[@"男", @"女"][indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.sexSelectBlock ? self.sexSelectBlock(1 - indexPath.row) : nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
